<!--添加修改角色信息-->
<template>
    <a-modal v-model="showAddTable" :mask-closable="false" :closable="false" :width="900"
             :title="title ? '新增门禁授权' : '编辑门禁授权'"
             :maskClosable="false"
             :destroyOnClose="true">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
          <a-form-item label="授权人：" prop="stuId" v-bind="formStyle" required >
            <a-select
                :showSearch="true"
                optionFilterProp="label"
                v-decorator="['stuId', { rules: [{ required: true, message: '选择授权人' }] }]"
                placeholder="请选择授权人" v-model="stuId"
                @select="changeStuList"
            >
              <a-select-option v-for="item in stuSelectList" :value="item.value"
                               :key="item.value" :label="item.label">{{item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="学生卡号：" prop="stuCardNum" v-bind="formStyle">
            <a-input
                placeholder="请输入学生卡号" ref="re"
                v-decorator="[
                        'stuCardNum',
                        {
                            rules: [
                                { required: true, message: '请输入英文、数字、@或_字符' },
                                { validator: this.verify.validateID3.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
            />
          </a-form-item>
          <a-form-item label="授权起止日期：" v-bind="formStyle">
            <a-date-picker
                :disabledDate="disabledStartDate"
                format="YYYY-MM-DD"
                v-model="startTime"
                placeholder="授权开始日期"
                style="width: 250px; float: left"
            />
            <span style="width: 50px; text-align: center; margin-left: 20px">-</span>
            <a-date-picker
                :disabledDate="disabledEndDate"
                format="YYYY-MM-DD"
                placeholder="授权结束日期"
                v-model="endTime"
                style="width: 250px; float: right"
            />
          </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
            <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import AFormItem from 'ant-design-vue/es/form/FormItem'
    import moment from 'moment'
    export default {
        name: 'addEdit',
        components: {
            AFormItem,
        },
        data () {
            return {
                verify,
                form: this.$form.createForm(this),
                showAddTable: false,
                input: false,
              labObj: {},
              name: "",
              stuId: "",
              laboratory: "",
              stuCardNum: "",
              labId: "",
              buildName: "",
              buildCode: "",
              roomCode: "",
              startTime: "",
              endTime: "",
            }
        },
        computed: {
            formStyle() {
                return this.$store.state.common.formStyle
            },
          labSelectList () {
            return this.$store.state.acManage.labSelectList
          },
          stuSelectList () {
            return this.$store.state.acManage.stuSelectList
          },
        },
        props: {

            title: {
                type: Boolean,
                required: true
            },
          auth: {
            type: Boolean,
            required: false
          },
          acDTO:{
            type: Object,
            required: true
          }
        },
        watch: {

        },
        methods: {
            /**
             * Function    : saveApply
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
            },
            /**
             * Function    : showEdit
             * Description : 编辑时显示
             * Input       : opt单个所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
              this.name = opt.name
              this.laboratory = opt.name
              this.stuCardNum = opt.stuCardNum
              this.stuId = opt.stuId
              this.labId = opt.recordId
              this.buildName = opt.buildName
              this.buildCode = opt.buildCode
              this.roomCode = opt.roomCode
              // this.startTime = moment (opt.startTime).format ('YYYY-MM-DD')
              // this.endTime =  moment (opt.endTime).format ('YYYY-MM-DD')
                this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick (opt,
                        ['stuCardNum','stuId','labId','laboratory' ]))
                })
            },

          changeLabList ( value ) {
              let _this = this;
              let labSelectList = _this.labSelectList;
              for (let index in labSelectList){
                if (labSelectList[index].value === value){
                  this.labObj = labSelectList[index].record
                }
              }
          },
          changeStuList ( value ) {
            let _this = this;
            let stuSelectList = _this.stuSelectList;
            for (let index in stuSelectList){
              if (stuSelectList[index].value === value){
                this.name = stuSelectList[index].record.name
                this.stuId = stuSelectList[index].record.id
              }
            }
          },
          searchStuList ( value ) {
              console.log(value)
            let _this = this;
            let stuSelectList = _this.stuSelectList;
            for (let index in stuSelectList){
              if (stuSelectList[index].value === value){
                this.name = stuSelectList[index].record.name
                this.stuId = stuSelectList[index].record.id
              }
            }
          },

          // filterStuOption(value, option){
          //   return option.label.indexOf(value) >= 0
          // },
          // filterLabOption(value, option){
          //   return option.label.indexOf(value) >= 0
          // },

          /**
           * Function    : disabledStartDate
           * Description : 对结束时间进行限制
           * Input       : startValue
           * Return      : None
           */
          disabledStartDate ( startValue ) {
            return startValue && startValue < moment().subtract(1, 'days')
          },
          /**
           * Function    : disabledEndDate
           * Description : 对结束时间进行限制
           * Input       : endValue
           * Return      : None
           */
          disabledEndDate ( endValue ) {
            const startTime = this.startTime
            if (startTime) {
              return startTime.valueOf () >= endValue.valueOf ()
            }
            return (
                endValue && endValue < moment().subtract(1, 'days')
            )
          },

            /**
             * Function    : saveApply
             * Description : 角色信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( ) {
              let _this = this;
                this.form.validateFields((err, value) => {
                    if (!err) {
                        let para = {}
                        para.name = _this.name
                        para.laboratory = _this.laboratory
                        para.stuCardNum = value.stuCardNum
                        para.stuId = _this.stuId
                        para.labId = _this.labId
                        para.buildName = _this.buildName
                        para.buildCode = _this.buildCode
                        para.roomCode = _this.roomCode
                        para.startTime = moment (_this.startTime).format ('YYYY-MM-DD')
                        para.endTime =  moment (_this.endTime).format ('YYYY-MM-DD')
                      if (!_this.title){
                        para.recordId = _this.id
                      }
                      _this.$store.dispatch (this.auth?'authAct':'saveAcAct', para).then (response => {
                          if (response.code === 0) {
                            _this.cancelApply()
                            _this.$emit ('init')
                            _this.$message.success (response.message)
                          } else {
                            _this.$message.error (response.message)
                          }
                      })
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : val
             * Return      : none
             */
            cancelApply ( ) {
                this.showAddTable = false
                let _this = this
              _this.clearForm()
                setTimeout(function() {
                    _this.form.resetFields()
                }, 100)
            },
          clearForm(){
            this.labObj = {}
            this.id = ''
            this.name = ''
            this.laboratory = ''
            this.stuCardNum = ''
            this.stuId = ''
            this.labId = ''
            this.buildName = ''
            this.buildCode = ''
            this.roomCode = ''
            this.startTime =''
            this.endTime = ''
          },

          initData(){
            this.$store.dispatch ('getLabListAct', {})
            this.$store.dispatch ('getStuListAct', {})
          }


        },

        created () {
          this.initData();
        }
    }
</script>
